<template>
  <div class="edit-username">
    <!-- 导航栏 -->
    <van-nav-bar
      title="编辑昵称"
      left-text="取消"
      right-text="确定"
      @click-left="cancel"
      @click-right="sureModifyName"
    />
    <!-- /导航栏 -->
    <div style="padding: 10px">
      <van-field
        v-model.trim="message"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入新昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { modifyProfile } from '@/api/userInfo.js'
export default {
  name: 'EditUserName',
  data () {
    return {
      message: this.name,
      messageCopy: this.name
    }
  },
  created () {
    // console.log(this.name, this.message)
  },
  props: {
    name: {
      type: String,
      required: true
    }
  },
  methods: {
    async sureModifyName () {
      if (this.name === this.message) {
        return this.$toast('未变化')
      }
      const data = {
        name: this.message
      }
      try {
        await modifyProfile(data)
        this.$emit('close')
        this.$emit('sure', this.message)
        this.$toast.success('更改成功')
      } catch (error) {
        this.$toast.fail('该名称不合法')
      }
    },
    cancel () {
      this.$emit('close')
      this.message = this.messageCopy
    }
  },
  computed: {},
  watch: {},
  components: {}
}
</script>

<style lang="less" scoped>
.van-popup {
  background: #f5f7f9;
}
</style>
